export default {
    namespaced:true,
    state:{
        name:"产品product",
        products:[],
    },
    actions:{
        async FETCH_PRODUCT({commit}){
            let res = await fetch('http://chst.vip/data/index.json').then(body => body.json())
            console.log(res)
            commit('SET_PRODUCT',res.data)
        }
        
    },
    getters:{},
    mutations:{
        SET_PRODUCT(state,payload){
            state.products = payload
            console.log(this.products)
        },
        
        DECREASE_INVENTORY(state, payload) {
            // console.log(payload)

            let target = state.products.find(item => item.id === payload.id)

            if(target) {
                target.inventory--
            }
        },
        INCREASE_INVENTORY(state, payload) {
            // console.log(payload)

            let target = state.products.find(item => item.id === payload.id)

            if(target) {
                target.inventory++
            }
        }
    },
}